//
//  AIOtherDataListCell.m
//  Mia
//
//  Created by Vill on 2024/2/23.
//

#import "AIOtherDataListCell.h"

@interface AIOtherDataListCell ()<UIPickerViewDataSource, UIPickerViewDelegate>

@property (weak, nonatomic) IBOutlet UIView *containView;
@property (nonatomic, strong) UIVisualEffectView *blurView;
@property (nonatomic, strong) UIButton *sureBtn; // 确认

@property (nonatomic, strong) UIPickerView *pickerView;
@property (nonatomic, strong) NSArray *nameArr;

@property (nonatomic, strong) NSString *selectItemStr;

@end

@implementation AIOtherDataListCell

- (UIVisualEffectView *)blurView{
    if(!_blurView){
        UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        _blurView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
        _blurView.alpha = 0.7;
        _blurView.backgroundColor = [UIColor colorHex:@"#FFFFFF" alpha:0.1];
    }
    return _blurView;
}

-(UIPickerView *)pickerView{
    if(!_pickerView){
        _pickerView = [UIPickerView new];
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
        _pickerView.backgroundColor = UIColor.clearColor;
    }
    return _pickerView;
}

+(instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *ID = @"AIOtherDataListCell";
    AIOtherDataListCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if(cell == nil){
       cell = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil].lastObject;
    }
    return cell;
}

- (UIButton *)sureBtn{
    if(!_sureBtn){
        _sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_sureBtn setImage:KImaged(@"ai_regis_choose_normal") forState:UIControlStateNormal];
//        [_sureBtn setImage:KImaged(@"ai_regis_choose_select") forState:UIControlStateSelected];
        [_sureBtn setImage:KImaged(@"ai_regis_choose_select") forState:UIControlStateNormal];
        [_sureBtn addTarget:self action:@selector(sureAction:) forControlEvents:UIControlEventTouchUpInside];
        _sureBtn.enabled = NO;
    }
    return _sureBtn;
}

// 确认
- (void)sureAction:(UIButton *)sender{
    if (self.otherDataSelectBlock){
        self.otherDataSelectBlock(self.dataSetModel.dataTitleId,self.selectItemStr);
    }
}

- (void)setDataSetModel:(AIRegisterDataSetModel *)dataSetModel{
    _dataSetModel = dataSetModel;
    self.nameArr = dataSetModel.contents;
//    NSLog(@"%@ --- %lu",self.nameArr,(unsigned long)self.nameArr.count);
    [self.pickerView reloadAllComponents];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
//    self.data = @[@"Health and Medical", @"Education and Academics", @"Business and Management", @"Technology and Engineering", @"Arts and Service",@"Current students"];

    self.containView.backgroundColor = [UIColor clearColor];
    self.containView.layer.cornerRadius = PTW(20);
    self.containView.layer.masksToBounds = YES;
    
    [self.containView addSubview:self.blurView];
    [self.blurView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.containView);
    }];
    
    [self.containView addSubview:self.pickerView];
    [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.containView);
    }];
    
    [self.containView addSubview:self.sureBtn];
    [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.trailing.mas_equalTo(self.containView);
        make.size.mas_equalTo(PTW(55));
    }];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return  50;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1; // 设置 UIPickerView 的列数
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.nameArr.count; // 设置每个列中的行数
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSString *selectedOption = self.nameArr[row]; // 获取用户选择的选项
//    NSLog(@"Selected Option: %@", selectedOption);
    self.sureBtn.enabled = YES;
    self.selectItemStr = selectedOption;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *pickerLabel = (UILabel *)view;
    if (pickerLabel == nil) {
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.font = [UIFont fontWithWeight:MTBold size:PTW(22)];
        pickerLabel.textAlignment = NSTextAlignmentCenter;
    }
    pickerLabel.text = self.nameArr[row]; // 设置每一行的文本
    pickerLabel.textColor = [UIColor whiteColor];
    return pickerLabel;
}


@end
